-- 读取请求参数
ngx.req.read_body()

-- 关闭redis链接=======================================
local function close_redis(red)  
    if not red then  
        return  
    end  
    local ok, err = red:close()  
    if not ok then  
        ngx.say("close redis error : ", err)  
    end  
end

-- 链接redis=======================================
local redis = require("resty.redis")  
  
--创建实例  
local red = redis:new()  
--设置超时（毫秒）  
red:set_timeout(1000)  
--建立连接  
local ip = "127.0.0.1"  
local port = 6379  
local ok, err = red:connect(ip, port)  
local res, err = red:auth("123456")


--获取请求的参数=======================================
local gcjson=require("cjson")

local args=ngx.req.get_body_data()
local arg=gcjson.decode(args)

-- 获取库存==============================================
local key = "key_kill_course_semaphore:"..arg["courseId"]
local resp, err = red:get(key)  
if not resp then  
    resp = 0 
end  

close_redis(red)

resp = tonumber(resp)

if resp <= 0 then 
	ngx.header.content_type = "application/json;charset=utf8"
	ngx.status = 200
	ngx.say("store zero") 
	ngx.exit(200)
end




